package org.springframework.samples.mvc.hello;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hello")
@SessionAttributes("helloBean")
public class HelloController {

	public HelloController(){
		
	}
	
	@Autowired
	@Qualifier("helloBean3")
	public void setHelloBean(HelloBean helloBean){
		System.out.println("setHelloBean() ...");
	}
	
	//@RequestMapping(method = RequestMethod.GET) 
	public String initForm(Model model){ 
		HelloBean helloBean = new HelloBean();
		
		model.addAttribute("helloBean",helloBean);
		
		return "/hello/Hello";
	}
	
	@RequestMapping(method = RequestMethod.GET) 
	public String initForm(@RequestParam("id") int id, Model model){ 
		
		System.out.println( "id = "+id );
		
		HelloBean helloBean = new HelloBean();
		
		model.addAttribute("helloBean",helloBean);
		
		return "/hello/Hello";
	}
	

	
	//@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
	@RequestMapping(method = RequestMethod.POST )
	public ModelAndView forGetAndPostRequest1(HttpServletRequest request, HelloBean helloBean){
		System.out.println( "forGetAndPostRequest1 ..." );
		
		System.out.println( "helloBean = "+helloBean );
		System.out.println( "helloBean.getName() = "+helloBean.getName() );
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/hello/Hello");
		modelAndView.addObject("message", "RequestMethod.POST !!!");
		
        // Populate the modelAndView object before return.
        return modelAndView;
	}
	
//	@RequestMapping(method = RequestMethod.GET )
//	public ModelAndView forGetAndPostRequest2(HttpServletRequest request){
//		System.out.println( "forGetAndPostRequest2 ..." );
//		
//		System.out.println( "aaa ..."+request.getParameter("aaa") );
//		
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.setViewName("/hello/Hello");
//		modelAndView.addObject("message", "RequestMethod.GET !!!");
//		
//        // Populate the modelAndView object before return.
//        return modelAndView;
//	}
//	
//	//@RequestMapping(value = {"/hello/{ownerId}"},method = RequestMethod.GET )
//	public ModelAndView forGetAndPostRequest3(@PathVariable String ownerId){
//		System.out.println( "forGetAndPostRequest3 ..." );
//		System.out.println( "ownerId = "+ownerId );
//		
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.setViewName("/hello/Hello");
//		modelAndView.addObject("message", "Hello All People 3!!!");
//		
//        // Populate the modelAndView object before return.
//        return modelAndView;
//	}
}
